package com.example.demo.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class ParamController {
    @RequestMapping("/m1")
    public String m1(String name) {
        return "name=" + name;
    }

    @RequestMapping("/m2")
    public String m2(String name,Integer age) {
        return "name=" + name + ", age=" + age;
    }

    @RequestMapping("/m3")
    public String m3(String name,int age){
        return "name="+name+",age="+age;
    }

    @RequestMapping("/m4")
    public String m4(@RequestBody Person person){
        return person.toString();
    }

    @RequestMapping("/m5")   //前端请求属性重命名为后端属性名，false指请求中不是必须传
    public String m5(@RequestParam(value = "name",required = false)String username){
        return "username "+username;
    }

    @RequestMapping("/m6")
    public String m6(String []array){
        return Arrays.toString(array)+","+array.length;
    }

    @RequestMapping("/m7")  //集合类传参需要用@RequestParam
    public String m7(@RequestParam List<String> list){
        return list.toString()+","+list.size();
    }

    @RequestMapping("/m8")
    public String m8(@RequestBody List<Person> list){
        return list.toString()+","+list.size();
    }

    @RequestMapping("/m9/{userid}/{age}")  //获取url路径中的一些值，不是指query string中参数值
    public String m9(@PathVariable Integer userid,@PathVariable Integer age){
        return "userid=" + userid + ", age=" + age;
    }

    @RequestMapping("/m10")  //上传文件
    public String m10(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("E:/下载/"+file.getOriginalFilename()));
        return "success";
    }

    @RequestMapping("/m11")
    public String m11(@RequestBody int[]user){
        return Arrays.toString(user)+","+user.length;
    }

    @RequestMapping("/m12")
    public String m12(HttpServletRequest req){
        Cookie[] cookies = req.getCookies();
        if(cookies != null){
            Arrays.stream(cookies).forEach(cookie -> System.out.println(cookie.getName()+","+cookie.getValue()));
            return "获取cookie成功";
        }
        return "没有cookie";

    }

    @RequestMapping("/m13")
    public String m13(@CookieValue String ckk){
        return ckk;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.setAttribute("username","kzf");
        return "success";

    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest req){
        HttpSession session = req.getSession(false);
        if(session == null){
            return "当前没有session对象";
        }
        String username = (String) session.getAttribute("username");
        return "username=" + username;
    }

    @RequestMapping("/getSession2")  //默认为true，required = false可以使得没有session时，不报错，返回体为空
    public String getSession2(@SessionAttribute(required = false)String username){
        return username;
    }

    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session){
        String username= (String) session.getAttribute("username");
        return username;
    }

    @RequestMapping("/getHeader")
    public String getHeader(@RequestHeader(value = "Accept-Encoding")String ae){
        return ae;
    }

    @RequestMapping("/getHeader1")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:"+userAgent;
    }
}


